﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XrmLibrary.MsCrm4Adapter.Extensions
{
    public static class ErrorInfoExtensions
    {
        public static Microsoft.Crm.Sdk.Messages.ErrorInfo ToCrm2011ErrorInfo(this Microsoft.Crm.Sdk.ErrorInfo crm4ErrorInfo)
        {
            Microsoft.Crm.Sdk.Messages.ErrorInfo crm2011ErrorInfo =
                new Microsoft.Crm.Sdk.Messages.ErrorInfo
                {
                    ErrorCode = crm4ErrorInfo.ErrorCode,
                    ResourceList = crm4ErrorInfo.ResourceList.ToCrm2011ResourceInfoCollection().ToArray<Microsoft.Crm.Sdk.Messages.ResourceInfo>()
                };

            return crm2011ErrorInfo;
        }

        public static IEnumerable<Microsoft.Crm.Sdk.Messages.ErrorInfo> ToCrm2011ErrorInfoCollection(this Microsoft.Crm.Sdk.ErrorInfo[] crm4ErrorInfo)
        {
            foreach (Microsoft.Crm.Sdk.ErrorInfo errorinfo in crm4ErrorInfo)
            {
                yield return errorinfo.ToCrm2011ErrorInfo();
            }
        }
    }
}
